@echo off
setlocal
pushd %~dp0..
call :GetNugetExe
call :RestorePackages
popd
exit /b 0

:GetNugetExe
call :DoCall ".\Script\GetNugetExe.cmd" "Download Nuget.exe if necessary"
goto :eof

:RestorePackages
call :DoCall ".\Script\nuget.exe restore -ConfigFile %~dp0..\..\Nuget.config %~dp0..\CloudStore.sln -PackagesDirectory %~dp0..\packages -verbosity quiet" "Restore Nuget packages"

::IF NOT DEFINED QBUILD_DISTRIBUTED (
::call :DoCall ".\Script\nuget.exe install -NoCache JetBrains.ReSharper.CommandLineTools -OutputDirectory .\packages" "Restore ReSharper tool package"
::)

:: Nuget Restore fails to hook up imports of MSBuild target/props from packages when targeting multiple frameworks.
:: We've worked around that in the .csproj files themselves by adding the imports explicitly, 
:: but NuGet Restore still complains by creating this .targets file with a warning for each project.
echo Removing warnings auto-generated by NuGet Restore:
del /S *.nuget.targets
del /S *.nuget.props
goto :eof

:DoCall
:: %1 = "full command to run with arguments in quotes"
:: %2 = "command description"
echo Executing %~2: %~1
call %~1
if ERRORLEVEL 1 (
    echo '*******'
    echo %~2
    echo %~1
    echo '*******'
    call :FailFromFunction 2>NUL
)
goto :eof
:FailFromFunction
popd
:: Yes, this looks weird
:: http://stackoverflow.com/questions/10534911/how-can-i-exit-a-batch-file-from-within-a-function 
()
exit /b 1
